package com.wk.springboot.controller;

import com.sun.javafx.collections.MappingChange.Map;
import com.wk.springboot.bean.UserBean;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.crypto.KeyGenerator;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname CacheController
 * @Description cache 调度类
 * @Date 2020/7/9 15:58
 * @Author by weikai
 */

@RestController
@RequestMapping("/cache")
public class CacheController {

  @Autowired
  SimpleCacheManager cacheManager;

  @RequestMapping("/user")
  @Cacheable(cacheNames = "user",key = "#name")
  public List<java.util.Map> user(@RequestParam String name) {
    System.out.println(name);
    List<java.util.Map> dataList = new LinkedList<>();
    for(int i=0;i<2;i++){
      java.util.Map map = new HashMap<String,String>();
      map.put(i,name+i);
      dataList.add(map);
    }
    return dataList;
  }

  @RequestMapping("/userNo")
  @Cacheable(cacheNames = {"user", "default"})
  public String userNo() {
    return "no paramters1";
  }

  @RequestMapping(value = "/user", method = RequestMethod.POST)
  @Cacheable(cacheNames = {"user"}, keyGenerator = "myKeyGenerator")
  public String userInfo(@RequestBody UserBean user) {
    return user.toString();
  }


  @RequestMapping(value = "/userCon", method = RequestMethod.POST)
  @Cacheable(cacheNames = {
      "user"}, condition = "T(org.apache.commons.lang.StringUtils).equals(#user.name,'lisi')", unless = "T(org.apache.commons.lang.StringUtils).equals(#result,'123456')")
  public String userInfoCon(@RequestBody UserBean user) {
    return user.getPassword();
  }


  @RequestMapping(value = "/userKey")
  @Cacheable(cacheNames = {"user"})
  public String userInfoAndKey(@RequestParam String name, @RequestParam String password) {
    return name + ";" + password;
  }

  @RequestMapping("/update")
  @CachePut(cacheNames = {"user"})
  public String updateCache(@RequestParam String name){
    return name;
  }

  @RequestMapping("/updateVoid")
  @CachePut(cacheNames = {"user"})
  public void updateCacheVoid(@RequestParam String name){

  }

  @RequestMapping("/remove")
  @CacheEvict(cacheNames = {"user"},key = "#name")
  public String removeCache(@RequestParam String name){
    return name;
  }

  @RequestMapping("/removeVoid")
  @CacheEvict(cacheNames = {"user"},key = "#name")
  public void removeCacheVoid(@RequestParam String name){
  }

  @RequestMapping("/exc")
  @Cacheable(cacheNames = {"user"},key = "#name")
  public String getException(@RequestParam String name){
    int i = 1/0;
    return "ok";
  }

  @RequestMapping("/null")
  @Cacheable(cacheNames = {"user"},key = "#name",unless = "#result.size()==0")
  public List<String> getNull(@RequestParam String name){
    List<String> list = new LinkedList<>();
    list.add("111");
//    System.out.println(list.size());
    return list;
  }

  @RequestMapping("getCache")
  public String getCache() {
    ConcurrentMapCache userCache = (ConcurrentMapCache) cacheManager.getCache("user");
    ConcurrentMapCache defaultCache = (ConcurrentMapCache) cacheManager.getCache("default");
    return "userCache:" + userCache.getNativeCache() + " ;defaltCache:" + defaultCache
        .getNativeCache();
  }
}
